<template>
  <div class="warpper">
    <slot v-if="hasAuth"></slot>
  </div>
</template>

<script>
import eventEmitter from '@/utils/EventEmitter';

export default {
  name: 'AuthComp',
  props: ['userAuth'],
  data() {
    return {
      buttonRole: [], // 存储当前用户的按钮权限
    };
  },
  computed: {
    hasAuth() {
      if (Array.isArray(this.userAuth)) {
        // 如果 userAuth 是数组，检查是否有任意一个权限匹配
        return this.userAuth.some(role => this.buttonRole.includes(role));
      } else if (typeof this.userAuth === 'string') {
        // 如果 userAuth 是字符串，直接检查是否包含
        return this.buttonRole.includes(this.userAuth);
      } else {
        console.error('userAuth 必须是字符串或数组');
        return false;
      }
    },
  },
  created() {
    // 监听事件，更新 buttonRole
    eventEmitter.on('userButtonRole', (roles) => {
      this.buttonRole = roles;
    });
  },
};
</script>